class Solution:
    def __init__(self):
        self.player2_source = 65536
        
    def stoneGameCore(self, piles:list,list1,list2,level):
        if sum(list2)>self.player2_source:
            return False
        if level==0:
            print(list1)
            print(list2)
            self.player2_source = min(self.player2_source,sum(list2))
            if sum(list1)>sum(list2):
                return True
            else:
                return False
        if level%2==1:
            stone1 = max(piles[0],piles[-1])
            i = piles.index(stone1)
            piles.pop(i)
            list1.append(stone1)
            result = self.stoneGameCore(piles,list1,list2,level-1)
            list1.remove(stone1)
            piles.insert(i,stone1)

            # stone2 = min(piles[0], piles[-1])
            # i = piles.index(stone2)
            # piles.pop(i)
            # list1.append(stone2)
            # result2 = self.stoneGameCore(piles, list1, list2, level - 1)
            # list1.remove(stone2)
            # piles.insert(i, stone2)
        else:
            # stone1 = max(piles[0], piles[-1])
            # i = piles.index(stone1)
            # piles.pop(i)
            # list2.append(stone1)
            # result1 = self.stoneGameCore(piles, list1, list2, level - 1)
            # list2.remove(stone1)
            # piles.insert(i, stone1)

            stone2 = min(piles[0], piles[-1])
            i = piles.index(stone2)
            piles.pop(i)
            list2.append(stone2)
            result = self.stoneGameCore(piles, list1, list2, level - 1)
            list2.remove(stone2)
            piles.insert(i, stone2)
        return result
    
    
    def stoneGame(self, piles):
        """
        :type piles: List[int]
        :rtype: bool
        """
        list1 = []
        list2 = []
        result = self.stoneGameCore(piles,list1,list2,len(piles)-1)
        
        return result
        
def main():
    sol = Solution()
    #print(sol.stoneGame([3,7,4,2]))

    print(sol.stoneGame([7,7,12,16,41,48,41,48,11,9,34,2,44,30,27,12,11,39,31,8,23,11,47,25,15,23,4,17,11,50,16,50,38,34,48,27,16,24,22,48,50,10,26,27,9,43,13,42,46,24]))
    
if __name__=="__main__":
    main()